<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/SpringMVC07/js/jquery.js"></script>
</head>
<body>
    <button id="btn1">前端给后端发送一个字符串（json字符串）</button>
    <br>
    <button id="btn2">前端给后端发送一个字符串表格形式</button>
    <br>
    <button id="btn3">前端给后端发送一个对象（表单格式）</button>
    <br>
    <button id="btn4">前端给后端发送一个对象（json字符串）</button>
    <br>
    <button id="btn5">前端给后端发送一个对象（对象）,后端用map集合接受</button>
    <br>
    <button id="btn6">
        前端给后端发用户数组
    </button>
</body>
<script>
    // 前端直接发了一个 单一字符串(非json格式),前端需要配置 contentType:"application/json" 表示向后端发送json字符串
    // 后端 用 @RequestBody + 任意名字的形参来接受
    $("#btn1").click(function(){
        $.ajax({
            url:"/SpringMVC07/json2/test1",
            type:"post",
            contentType:"application/json",//告知后端 我给你发的是一个json字符串
            data:"zhangsan",
            success:function(data){
                console.log(data);
            }
        })
    })

    // 前端向后端发送了一个 json格式的对象，此时等效于从表单发起请求,不需要ct:application/json
    // 后端不需要使用 @RequestBody
    $("#btn2").click(function(){
        $.ajax({
            url:"/SpringMVC07/json2/test2",
            type:"post",
            data:{"username":"zhangsan"},
            success:function(data){
                console.log(data);
            }
        })
    })


    $("#btn3").click(function(){
        $.ajax({
            url:"/SpringMVC07/json2/test3",
            type:"post",
            data:{"username":"zhangsan",
                    "password":"123456",
                    "userAge":21,
                    "birthday":"2020-3-3",
                    "email":"77994322@qq.com"
                },
            success:function(data){
                console.log(data);
            }
        })
    })

    $("#btn4").click(function(){
        $.ajax({
            url:"/SpringMVC07/json2/test4",
            type:"post",
            data:JSON.stringify({"username":"zhangsan",
                    "password":"123456",
                    "userAge":21,
                    "birthday":"2020-3-3",
                    "email":"77994322@qq.com"
                }),
            contentType: "application/json",
            success:function(data){
                console.log(data);
            }
        })
    })

    $("#btn5").click(function(){
        $.ajax({
            url:"/SpringMVC07/json2/test5",
            type:"post",
            data:JSON.stringify({"username":"zhangsan",
                "password":"123456",
                "userAge":21,
                "birthday":"2020-3-3",
                "email":"77994322@qq.com"
            }),
            contentType: "application/json",
            success:function(data){
                console.log(data);
            }
        })
    })

    $("#btn6").click(function(){
        $.ajax({
            url:"/SpringMVC07/json2/test6",
            type:"get",
            data:JSON.stringify([{
                "username":"zhangsan",
                "password":"1234567"
            },{
                "username":"lisi",
                "password":"654321"
            }]),
            contentType: "application/json",
            success:function(data){
                console.log(data);
            }
        })
    })


</script>
</html>